﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public static class TextureHelper
{
    /// <summary>
    /// 保存图集中的图片
    /// </summary>
    /// <param name="_atlas">图片所在的图集</param>
    /// <param name="_path">要保存的路径</param>
    //public static void SaveSpriteAsTexture(NGUIAtlas _atlas, string _path)
    //{
    //    if (null != _atlas && null != _atlas.spriteList)
    //    {
    //        if (!System.IO.Directory.Exists(_path))
    //        {
    //            System.IO.Directory.CreateDirectory(_path);
    //        }
    //        var spriteList = _atlas.spriteList;
    //        for (int i = 0, max = spriteList.Count; i < max; ++i)
    //        {
    //            if (null == spriteList[i])
    //            {
    //                continue;
    //            }

    //            NGUISettings.atlas = _atlas;
    //            var se = UIAtlasMaker.ExtractSprite(_atlas, spriteList[i].name);
    //            if (se != null)
    //            {
    //                byte[] bytes = se.tex.EncodeToPNG();
    //                var _savePath = $"{_path}/{spriteList[i].name}.png";
    //                System.IO.File.WriteAllBytes(_savePath, bytes);
    //                if (se.temporaryTexture)
    //                    Object.DestroyImmediate(se.tex);
    //            }
    //        }
    //    }
    //}

    //[MenuItem("Tools/UpgradeAtlas")]
    //public static void UpdateAtlas()
    //{
    //    var assets = AssetDatabase.FindAssets("t:prefab", new string[] { "Assets/Res/chart" });
    //    var dir = $"{Application.dataPath}/Res/Atlas";
    //    for (int i = 0, max = assets.Length; i < max; ++i)
    //    {
    //        var assetPath = AssetDatabase.GUIDToAssetPath(assets[i]);
    //        var go = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
    //        if (null != go)
    //        {
    //            var atlas = go.GetComponent<UIAtlas>();
    //            if (null != atlas)
    //            {
    //                var path = $"Assets/Res/Atlas/{System.IO.Path.GetFileNameWithoutExtension(assetPath)}.asset";
    //                if (!System.IO.Directory.Exists(dir))
    //                {
    //                    System.IO.Directory.CreateDirectory(dir);
    //                }
    //                NGUISettings.currentPath = path;

    //                var asset = ScriptableObject.CreateInstance<NGUIAtlas>();
    //                asset.spriteList = atlas.spriteList;
    //                asset.spriteMaterial = atlas.spriteMaterial;

    //                var atlasName = path.Replace(".asset", "");
    //                atlasName = atlasName.Substring(path.LastIndexOfAny(new char[] { '/', '\\' }) + 1);
    //                asset.name = atlasName;

    //                AssetDatabase.DeleteAsset(path);
    //                AssetDatabase.CreateAsset(asset, path);
    //                AssetDatabase.SaveAssets();
    //                AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);

    //                asset = AssetDatabase.LoadAssetAtPath<NGUIAtlas>(path);
    //                NGUISettings.atlas = asset;
    //                Selection.activeObject = NGUISettings.atlas as Object;

    //                if (asset != null)
    //                {
    //                    atlas.replacement = asset;
    //                    atlas.MarkAsChanged();
    //                }
    //            }
    //        }
    //    }
    //}

    //[MenuItem("Assets/NGUI/Tools/ExtractAtlas", false, 1)]
    //public static void Extra()
    //{
    //    if (Selection.objects.Length > 0)
    //    {
    //        var extractPath = $"{Application.dataPath}/../../Cache/Chart";
    //        for (int i = 0, max = Selection.objects.Length; i < max; ++i)
    //        {
    //            if (Selection.objects[i] is NGUIAtlas nguiAtlas)
    //            {
    //                var texPath = AssetDatabase.GetAssetPath(nguiAtlas.spriteMaterial.mainTexture);
    //                TextureImporter textureImporter = AssetImporter.GetAtPath(texPath) as TextureImporter;
    //                if (null != textureImporter)
    //                    textureImporter.isReadable = true;
    //                AssetDatabase.ImportAsset(texPath);
    //                SaveSpriteAsTexture(nguiAtlas, $"{extractPath}/{nguiAtlas.spriteMaterial.mainTexture.name}");
    //                if (null != textureImporter)
    //                    textureImporter.isReadable = false;
    //                AssetDatabase.ImportAsset(texPath);
    //            }
    //        }
    //    }
    //}
}
